import clientUtils from "@/utils/app/clientUtils";
import appUtils from "@/utils/app/appUtils";
const useAppSdk = () => {
	if (clientUtils.isAPP())
		appUtils.sendParamsByFn({ notReload: true }, "isReloadWeb");
	const startScanner = (callback: (res: ScannerResponse) => void) => {
		window["app_scanner_callback"] = (res: string) => {
			try {
				callback(JSON.parse(res) as ScannerResponse);
			} catch (e) {
				console.log(e);
			}
		};
		appUtils.sendParamsByFn("", "startScanner");
	};
	const startLocation = (callback: (res: locationRespone) => void) => {
		window["app_location_callback"] = (res: string) => {
			try {
				callback(JSON.parse(res) as locationRespone);
			} catch (e) {
				console.log(e);
			}
		};
		appUtils.sendParamsByFn("", "startLocation");
	};

	const downloadFile = (params: downloadFileParams) => {
		appUtils.sendParamsByFn(params, "downloadFile");
	};
	const offlineToPay = (params: offlineToPayParams) => {
		appUtils.sendParamsByFn(params, "offlineToPay");
	};
	const initShareData = (params: initShareDataParams) => {
		appUtils.sendParamsByFn(params, "initShareData");
	};
	return {
		startScanner,
		startLocation,
		downloadFile,
		offlineToPay,
		initShareData,
		isApp: clientUtils.isAPP(),
		isMiniProgram: clientUtils.isMiniProgram(),
		isWeChat: clientUtils.isWeChat(),
	};
};
export { useAppSdk };
export default useAppSdk;
